import { _decorator } from 'cc';
const { ccclass, property } = _decorator;
import { Config } from './config';

@ccclass('Http')
export class Http {
    static api = Config.apiDomain()

    static get(token:string, survivalId:number) {

        let url = Http.api + "api/v1/game/survival/lastDetail?type=1"
        if (survivalId != 0) {
            url += "&survivalId=" + survivalId
        }

        return fetch(
            url,
            {
                method: 'GET',
                headers:{
                    "authorization" : token
                }
            })
            .then((response) => response.json())
            .catch (error => {
                console.error("Error:", error);
            })
    }

    static enter(token:string, survivalId:number) {
        return fetch(
            Http.api + "api/v1/game/survival/enter",
            {
                method: 'POST',
                headers:{
                    "Content-Type": "application/json",
                    "authorization" : token
                },
                body: JSON.stringify({"survivalId":survivalId})
            })
            .then((response) => response.json())
            .catch (error => {
                console.error("Error:", error);
            })
    }

    static join(token:string, num:number, survivalId:number, roomId:number) {
        return fetch(
            Http.api + "api/v1/game/survival/join",
            {
                method: 'POST',
                headers:{
                    "Content-Type": "application/json",
                    "authorization" : token
                },
                body: JSON.stringify({"survivalId":survivalId, "roomId":roomId, "betAmount":num})
            }).then((response) => response.json())
    }

    static changeRoom(token:string, num:number, survivalId:number, roomId:number) {
        return fetch(
            Http.api + "api/v1/game/survival/changeRoom",
            {
                method: 'POST',
                headers:{
                    "Content-Type": "application/json",
                    "authorization" : token
                },
                body: JSON.stringify({"survivalId":survivalId, "roomId":roomId, "betAmount":num})
            })
            .then((response) => response.json())
            .catch (error => {
                console.error("Error:", error);
            })
    }

    static joinTest(num:number, survivalId:number, roomId:number) {
        return fetch(
            Http.api + "api/v1/game/survival/join",
            {
                method: 'POST',
                headers:{
                    "Content-Type": "application/json",
                    "authorization" : "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJiOTZmMjYyZi0zN2I2LTRkMDUtYWYxMS1lNTIwNjBkY2IxMmMiLCJpZCI6IjEwMDAxODgiLCJleHAiOjE2OTc3ODkxMTMsImlhdCI6MTY5NTE5NzExMzI3Mn0.LT9Kqj_eJb211kxuPDxCCz3lQDNZCO8HWyy9JeS4pIwWRq5UPDAXD8ufM6MFHRrKUsPIEBdhFQ6gcjosTyQ55jbO6KoQTbp6NxIbiJ5iSeih1X18DbLvrpsrfNCb6P1-NNhXvPdlWS5VK7dh0PdLwflmoBeOiLveGzvfAVyiGfd7FFAhniJF0RZM2Y4Ay7MKSbUUVfPxL0mLRTu5kB6aMFcaQDbb5mUovoZKy2vkB_lFkpmrds1p6Idyxu73ylq9htW5TfKWW-kCBI4LOzaB96CHMTVLXyfUGGqxAnU6i5FiVsY3An50Rbm9wBTZoX9I7euFKs_djGfGjQl_0o5gVA"
                },
                body: JSON.stringify({"survivalId":survivalId, "roomId":roomId, "betAmount":num})
            }).then((response) => response.json())
    }
}

